package com.example.sneakerappforme1105.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.sneakerappforme1105.GoodsDetailActivity;
import com.example.sneakerappforme1105.R;
import com.example.sneakerappforme1105.bean.Goods;
import com.example.sneakerappforme1105.bean.Order;
import com.example.sneakerappforme1105.bean.OrderItem;

import java.util.HashMap;
import java.util.List;

public class SaleAdapter extends RecyclerView.Adapter{

    // 查询所有订单
    private Context mContext;
    private List<Order> mList;
    private HashMap<Long,List<OrderItem>> map;   // 订单id -> 订单项
    private HashMap<Long, Goods> mGoodsMap;
    private SaleAdapter.OnButtonListener mButtonListener;
    private SaleAdapter.OnDeleteListener mDeleteListener;



    // 定义一个循环视图列表项的长按监听器接口
    public interface OnButtonListener {
        void onButtonClick(View view, int position);

    }

    public interface OnDeleteListener {
        void delete(View view, int position);
    }

    public void setButtonListener(SaleAdapter.OnButtonListener mListener){
        this.mButtonListener = mListener;
    }
    public void setDeleteListener(SaleAdapter.OnDeleteListener mDeleteListener){this.mDeleteListener = mDeleteListener;}

    public SaleAdapter(Context mContext, List<Order>mList, HashMap<Long,List<OrderItem>>map, HashMap<Long, Goods> mGoodsMap){
        this.mContext = mContext;
        this.mList = mList;
        this.map = map;
        this.mGoodsMap = mGoodsMap;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_ready,viewGroup,false);
        return new SaleAdapter.ItemHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        Order item = mList.get(position);   // 能够得到订单id
        SaleAdapter.ItemHolder holder = (SaleAdapter.ItemHolder) viewHolder;


        holder.bt_fahuo.setOnClickListener(view -> {
            if(mButtonListener!=null){
                Log.d("OrderAdapter", "按钮点击，位置: " + position);
                mButtonListener.onButtonClick(view,holder.getAdapterPosition());
            }
        });

        holder.bt_del.setOnClickListener(view -> {
            if(mDeleteListener!=null){
                Log.d("OrderAdapter", "按钮点击，位置: " + position);
                mDeleteListener.delete(view,holder.getAdapterPosition());
            }
        });

        holder.ll_address.removeAllViews();
        View addressView = LayoutInflater.from(mContext).inflate(R.layout.item_address,null);
        TextView tv_address = addressView.findViewById(R.id.tv_address);
        ImageView iv_xg = addressView.findViewById(R.id.iv_xg);
        iv_xg.setVisibility(View.GONE);
        TextView tv_username = addressView.findViewById(R.id.tv_username);
        TextView tv_tel = addressView.findViewById(R.id.tv_tel);
        Button bt_delAddress = addressView.findViewById(R.id.bt_delAddress);
        bt_delAddress.setVisibility(View.GONE);
        String[] address = item.getAddress().split(" ");

        if(address.length<3){
            tv_address.setText("深圳大学粤海校区致真楼");
            tv_username.setText("莫家伟");
            tv_tel.setText("123456789");
        }else{
            tv_address.setText(address[0]);
            tv_username.setText(address[1]);
            tv_tel.setText(address[2]);
        }
        holder.ll_address.addView(addressView);

        // 当前订单关联的订单项
        List<OrderItem> orderItems = map.get(item.getOrderId());
        OrderItemAdapter orderItemAdapter = new OrderItemAdapter(mContext,orderItems,mGoodsMap);
        LinearLayoutManager llm = new LinearLayoutManager(mContext);
        holder.rv_purchaseItem.setLayoutManager(llm);
        holder.rv_purchaseItem.setAdapter(orderItemAdapter);
        holder.tv_orderId.setText("订单号:"+String.valueOf(10000+item.getOrderId()));
        holder.tv_sum.setText("￥"+String.valueOf(GoodsDetailActivity.formatDouble(item.getTotalAmount())));

        if(item.getStatus().equals("paid")){
            // 按钮出现
            holder.bt_fahuo.setVisibility(View.VISIBLE);
            holder.tv_status.setVisibility(View.GONE);
        }else{
            // 按钮出现
            holder.bt_fahuo.setVisibility(View.GONE);
            holder.tv_status.setVisibility(View.VISIBLE);
            holder.bt_del.setVisibility(View.VISIBLE);
        }


    }

    private class ItemHolder extends RecyclerView.ViewHolder {
        private TextView tv_orderId;
        private LinearLayout ll_address;
        private RecyclerView rv_purchaseItem;
        private TextView tv_sum;
        private TextView tv_status;
        private Button bt_fahuo;
        private Button bt_del;


        public ItemHolder(View itemView) {
            super(itemView);
            ll_address = itemView.findViewById(R.id.ll_address);
            tv_sum = itemView.findViewById(R.id.tv_sum);
            rv_purchaseItem = itemView.findViewById(R.id.rv_purchaseItem);
            tv_sum = itemView.findViewById(R.id.tv_sum);
            tv_status = itemView.findViewById(R.id.tv_status);
            bt_fahuo = itemView.findViewById(R.id.bt_fahuo);
            tv_orderId = itemView.findViewById(R.id.tv_orderId);
            bt_del = itemView.findViewById(R.id.bt_del);
        }



    }



    @Override
    public int getItemCount() {
        return mList.size();
    }


}
